package org.hot100.h003;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yu
 * @Date 2022/3/4
 */
public class Solution {

    public static void main(String[] args) {

        String str = "abcabcbb";
        //String str = " ";
        //String str = "a";
        //String str = "qrsvbspk";
        //String str = "nfpdmpi";
        //String str = "tmmzuxt";

        Solution solution = new Solution();
        System.out.println(solution.lengthOfLongestSubstring5(str));

    }

    /**
     * 原创
     *
     * @param s
     * @return int
     */
    public int lengthOfLongestSubstring(String s) {
        int r=0,max=0;
        int[] nums=new int[128];
        StringBuilder res=new StringBuilder();

        while(r<s.length()){
            char c=s.charAt(r++);
            if (nums[c]==0){
                nums[c]++;
                res.append(c);
                max=Math.max(max,res.length());
            }else{
                nums[c]++;
                while(nums[c]>=2){
                    if (res.charAt(0)!=c){
                        nums[res.charAt(0)]--;
                        res.deleteCharAt(0);
                    }else{
                        nums[c]--;
                        res.deleteCharAt(0);
                    }
                }
                res.append(c);
            }
        }
        return max;
    }

    /**
     * 原创2。0
     *
     * @param s
     * @return int
     */
    public int lengthOfLongestSubstring2(String s) {
        if (s.length() == 0 || s.length() == 1) {
            return s.length() == 0 ? 0 : 1;
        }

        Map<Character, Integer> map = new HashMap<>();
        int temp = 0, sum = 0;

        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i)) && temp <= map.get(s.charAt(i))) {
                temp = map.get(s.charAt(i)) + 1;
            }
            map.put(s.charAt(i), i);
            sum = Math.max(sum, i - temp + 1);
        }

        return sum;
    }

    public int lengthOfLongestSubstring3(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int temp = 0, sum = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i)) && temp <= map.get(s.charAt(i))) {
                temp = map.get(s.charAt(i)) + 1;
            }
            map.put(s.charAt(i), i);
            sum = Math.max(sum, i - temp + 1);
        }

        return sum;
    }

    // abcabcbb
    public int lengthOfLongestSubstring4(String s) {

        Map<Character, Integer> map = new HashMap<>();
        int temp = 0, size = 0;

        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i)) && temp <= map.get(s.charAt(i))) {
                temp = map.get(s.charAt(i)) + 1;
            }
            map.put(s.charAt(i), i);
            size = Math.max(size, i - temp + 1);
        }
        return size;
    }

    public int lengthOfLongestSubstring5(String s) {
        char[] chars = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        int temp = 0, size = 0;
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i]) && temp <= map.get(chars[i])) {
                temp = map.get(chars[i]) + 1;
            }
            map.put(chars[i], i);
            size = Math.max(size, i - temp + 1);
        }
        return size;
    }

}
